package com.example.Sao.Tools;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import static org.greenrobot.essentials.Base64.ENCODE;

/**
 * @ProjectName: scan
 * @Package: com.example.Sao.Tools
 * @ClassName: DESUtils
 * @Description: java类作用描述
 * @Author: 作者名
 * @CreateDate: 2020/5/4 13:45
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/5/4 13:45
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class DesCBCUtil {
    public static byte[] encryptDES(byte[]encryptString, String encryptKey)
            throws Exception {
        //使用指定密钥构造IV
        IvParameterSpec iv = new IvParameterSpec(encryptKey.getBytes("US-ASCII"));
        //根据给定的字节数组和指定算法构造一个密钥。
        SecretKeySpec skeySpec = new SecretKeySpec(encryptKey.getBytes("US-ASCII"), "DES");
        //返回实现指定转换的 Cipher 对象
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        //加密初始化
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
        //加密同时解码成字符串返回
        return cipher.doFinal(encryptString);
    }

    public static String decryptDES(byte[] decodeString, String decodeKey) throws Exception{
            //使用指定密钥构造IV
            IvParameterSpec iv = new IvParameterSpec(decodeKey.getBytes("US-ASCII"));
            //根据给定的字节数组和指定算法构造一个密钥。
            SecretKeySpec skeySpec = new SecretKeySpec(decodeKey.getBytes("US-ASCII"), "DES");
            //返回实现指定转换的 Cipher 对象
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            //解密初始化
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            //解码返回
            byte[] byteMi = decodeString;
            byte decryptedData[] = cipher.doFinal(byteMi);
//        Log.d("des","解密字节数"+decryptedData.length);
            return new String(decryptedData,"UTF-8");
    }

    public static String str2HexStr(String str)
    {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++)
        {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            sb.append(' ');
        }
        return sb.toString().trim();
    }
}
